package cn.shop.admin.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.RandomUtil;
import cn.shop.admin.entity.dictionaryModule.Dictionary;
import cn.shop.admin.entity.rateModule.Rate;
import cn.shop.admin.service.DictionaryService;
import cn.shop.utils.ApiResult;
import cn.shop.utils.annotation.OperationLog;
import cn.shop.utils.enums.OperationType;
import cn.shop.validation.Save;
import cn.shop.validation.Update;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 费率
 *
 * @author jzw
 * @since 2021-11-03 15:11:58
 */
@Validated
@RestController
@RequestMapping("/api/v1/rate")
public class RateController {

	/**
	 * 服务对象
	 */
	@Resource
	private DictionaryService dictionaryService;

	/**
	 * 字典表中 费率的顶级id
	 */
	private final Long RATE_PARENT_ID = 1465970419053453314L;

	/**
	 * 查询所有费率
	 *
	 * @return cn.shop.utils.ApiResult<java.util.List < java.lang.Object>>
	 * @author jzw
	 * @since 2021/11/4 13:24
	 */
	@GetMapping("queryAll")
	public ApiResult<List<Map<String, Object>>> queryAllRate() {
		QueryWrapper<Dictionary> query = Wrappers.query();
		query.select("id", "name", "attach1 rate");
		query.eq("parent_id", RATE_PARENT_ID);
		return ApiResult.success(dictionaryService.listMaps(query));
	}

	/**
	 * 新增数据
	 *
	 * @param rate 实体对象
	 * @return 新增结果
	 */
	@PostMapping("insert")
	@OperationLog(desc = "新增费率模块", type = OperationType.UPDATE)
	public ApiResult<Map<String, Object>> insert(@Validated(Save.class) @RequestBody Rate rate) {
		Dictionary dictionary = new Dictionary();
		dictionary.setName(rate.getName());
		dictionary.setAttach1(String.valueOf(rate.getRate()));
		dictionary.setParentId(RATE_PARENT_ID);
		dictionary.setCode(RandomUtil.randomStringUpper(10));
		return ApiResult.result(dictionaryService.save(dictionary));
	}

	/**
	 * 修改数据
	 *
	 * @param rate 实体对象
	 * @return 修改结果
	 */
	@PutMapping("update")
	@OperationLog(desc = "修改费率模块", type = OperationType.UPDATE)
	public ApiResult<Map<String, Object>> update(@Validated(Update.class) @RequestBody Rate rate) {
		Dictionary dictionary = new Dictionary();
		dictionary.setId(rate.getId());
		dictionary.setName(rate.getName());
		dictionary.setAttach1(Convert.toStr(rate.getRate()));
		return ApiResult.result(dictionaryService.updateById(dictionary));
	}

	/**
	 * 删除单条数据
	 *
	 * @param id 主键
	 * @return 删除结果
	 */
	@DeleteMapping("delete/{id}")
	public ApiResult<Map<String, Object>> delete(@PathVariable Long id) {
		Dictionary dictionary = new Dictionary();
		dictionary.setId(id);
		return ApiResult.result(dictionaryService.removeById(dictionary));
	}

}

